﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using BookStores.DTO.Request;
using Utility.Extensions;
namespace BookStores.DTO.DtoMapping
{
    /// <summary>
    /// 实体,DTO映射关系配置表
    /// </summary>
    public class EntityMapperDtoConfig: Shared.Contracts.IAutoMapperConfig
    {
        public EntityMapperDtoConfig()
        {
            CreateMap<BookStores.Entity.Books, BookInfo>()
                .ConstructUsing((BookStores.Entity.Books t) => new BookInfo
                {
                    ID = t.ID,
                    Book_Name = t.Book_Name,
                    Book_Publish_Status = t.Book_Publish_Status,
                    Book_Recommend_Status = t.Book_Recommend_Status,
                    Book_SerialNo = t.Book_SerialNo,
                    Book_Barcode = t.Book_Barcode,
                    Book_Brief = t.Book_Brief,
                    Book_Content = t.Book_Content,
                    Book_Publishing = t.Book_Publishing,
                    Book_Publishing_Time = t.Book_Publishing_Time.Value.Date,
                    Book_Purchasing_Price = t.Book_Purchasing_Price.Value,
                    Book_Price = t.Book_Price.Value
                })
               .ForMember(dest => dest.Book_Tag, opt => opt.MapFrom(src => src.Book_Tag.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Language, opt => opt.MapFrom(src => src.Book_Language.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Author, opt => opt.MapFrom(src => src.Book_Author.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Series, opt => opt.MapFrom(src => src.Book_Series.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));


            CreateMap<BookInfo, BookStores.Entity.Books>()
                .ConstructUsing((BookInfo t) => new BookStores.Entity.Books
                {
                    ID = t.ID,
                    Book_Name = t.Book_Name.StrTrim(),
                    Book_Publish_Status = t.Book_Publish_Status.StrTrim(),
                    Book_Recommend_Status = t.Book_Recommend_Status.StrTrim(),
                    Book_SerialNo = t.Book_SerialNo.StrTrim(),
                    Book_Barcode = t.Book_Barcode.StrTrim(),
                    Book_Picture = t.Book_Priture_List != null ? t.Book_Priture_List.FirstOrDefault().StrTrim() : "",
                    Book_Brief = t.Book_Brief.StrTrim(),
                    Book_Content = t.Book_Content.StrTrim(),
                    Book_Publishing = t.Book_Publishing.StrTrim(),
                    Book_Publishing_Time = t.Book_Publishing_Time,
                    Book_Purchasing_Price = t.Book_Purchasing_Price,
                    Book_Price = t.Book_Price
                })
               .ForMember(dest => dest.Book_Tag, opt => opt.MapFrom(src => string.Join(',', src.Book_Tag)))
               .ForMember(dest => dest.Book_Language, opt => opt.MapFrom(src => string.Join(',', src.Book_Language)))
               .ForMember(dest => dest.Book_Author, opt => opt.MapFrom(src => string.Join(',', src.Book_Author)))
               .ForMember(dest => dest.Book_Series, opt => opt.MapFrom(src => string.Join(',', src.Book_Series)));



            CreateMap<BookStores.Entity.Books, DTO.Response.BookInfo>()
                .ConstructUsing((BookStores.Entity.Books t) => new DTO.Response.BookInfo
                {
                    ID = t.ID,
                    Book_Name = t.Book_Name.StrTrim(),
                    Book_Publish_Status = t.Book_Publish_Status.StrTrim(),
                    Book_Recommend_Status = t.Book_Recommend_Status.StrTrim(),
                    Book_SerialNo = t.Book_SerialNo.StrTrim(),
                    Book_Barcode = t.Book_Barcode.StrTrim(),
                    Book_Publishing = t.Book_Publishing.StrTrim(),
                    Book_Publishing_Time = t.Book_Publishing_Time.Value.Date
                })
               .ForMember(dest => dest.Book_Tag, opt => opt.MapFrom(src => src.Book_Tag.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Language, opt => opt.MapFrom(src => src.Book_Language.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Author, opt => opt.MapFrom(src => src.Book_Author.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)))
               .ForMember(dest => dest.Book_Series, opt => opt.MapFrom(src => src.Book_Series.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)));


        }
    }
}
